package com.frejoys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.dao.entity.UserCode;
import com.frejoys.dao.mapper.UserCodeMapper;
import com.frejoys.service.UserCashService;
import com.frejoys.service.UserCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserCodeServiceImpl extends ServiceImpl<UserCodeMapper, UserCode> implements UserCodeService {

    @Override
    public Long codeByUserId(String code) {

        if (code.length() < 10){
            LambdaQueryWrapper<UserCode> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(UserCode::getUserId).eq(UserCode::getCode,code);
            UserCode userCode = getOnly(wrapper);

            if (userCode == null){
                throw new GeneralException(HttpStatus.userCodeError);
            }

            return userCode.getUserId();
        }else {
            //校验邀请码是否错误
            if (code.matches(".*[a-zA-Z].*")){
                throw new GeneralException(HttpStatus.userCodeError);
            }
            if (code.length() >= 20){
                throw new GeneralException(HttpStatus.userCodeError);
            }
        }
        return Long.valueOf(code);
    }

}
